<?php
    function getTree($data)
    {
        $html = '';
        foreach($data as $k => $v)
        {
            if(isset($v['type_child']))
            {   //父亲找到儿子
                $html .= '<li><a tname="type_ids" tvalue="'.$v['type_id'].'">'.$v['type_name'].'</a>';
                $html .= getTree($v['type_child']);
                $html .= '</li>';
            }else{
                $html .= '<li><a tname="type_ids" tvalue="'.$v['type_id'].'">'.$v['type_name'].'</a></li>';
            }
        }
        return '<ul>'.$html.'</ul>';
    }

    $parent_array = array();//记录每一个父节点的地址引用
    $tree_array = array();//整颗树的数组

    foreach($menu_arr as $v){

        $node_num = ($v['right_value']-$v['left_value'] - 1)/2;

        if($v['parent_type_id']){//如果没有parent_cate_id则是顶层

            $parent_array[$v['parent_type_id']]['type_child'][$v['type_id']] = $v;

            
            if($node_num > 0){

                $parent_array[$v['type_id']] = &$parent_array[$v['parent_type_id']]['type_child'][$v['type_id']];
                $parent_array[$v['type_id']]['type_child'] = array();
            }

        }else{
            $tree_array[$v['type_id']] = $v;

            if($node_num > 0){

                $parent_array[$v['type_id']] = &$tree_array[$v['type_id']];
                $parent_array[$v['type_id']]['cate_child'] = array();
            }
        }
    }
    //p($tree_array);
?>
<div class="pageContent">
	<div class="pageFormContent" layoutH="60">
        <ul class="tree treeFolder expand" id="_select_type">
            <li><a tname="type_ids" tvalue="0">发现宝贝</a>
                <?php echo getTree($tree_array);?>
            </li>
        </ul>
	</div>
	<div class="formBar">
		<ul>
			<li><a class="button"  href="javascript:void(0);" onclick="cateTreeCallBack();"><span>确认</span></a></li>
			<li><div class="button"><div class="buttonContent"><button class="close" type="button">关闭</button></div></div></li>
		</ul>
	</div>
</div>
<script type="text/javascript">
var olda = '';

(function($){
    $('#_select_type').find('a').click(function(){
        if(olda){
            olda.text(olda.text().replace('(√)',''));
        }

        $(this).text($(this).text()+'(√)');
        olda = $(this);
    });
    
    var pdialog = $("body").data('add_baobeitype');
    var sids = $('input[name="parent_type_id"]', pdialog).val();
    
    setTimeout(function(){//这里代码有延迟事件，所以用setTimeout调整执行顺序
        var dialog = $("body").data('type_tree');
        var el = dialog.find("a[tvalue='"+sids+"']");

        el.text(el.text()+'(√)');
        olda = el;
    },0);

})(jQuery);

function cateTreeCallBack(){
    if(olda){

        var pdialog = $("body").data('add_baobeitype');
        $('input[name="parent_type_id"]', pdialog).val(olda.attr('tvalue'));
        $('input[name="parent_type_name"]', pdialog).val(olda.text().replace('(√)',''));
        $.pdialog.closeCurrent();
    }else{
        alertMsg.error('必须选择一个宝贝分类！');
    }
}
</script>